<template>
  <div class="main-container">
    <div class="item-content">
      <div class="form-content" style="margin-bottom: 20px">
        <div class="item-header">
          <div class="subheading">参培人员花名册</div>
          <div>
            <el-select
              @change="change"
              v-model="selectValue"
              placeholder="请选择查看类型"
              style="width: 100%"
            >
              <el-option label="全部" value="3" key="3"> </el-option>
              <el-option label="未审核" value="1" key="1"> </el-option>
              <el-option label="已审核" value="2" key="2"> </el-option>
            </el-select>
          </div>
        </div>
        <!-- 表格展示 -->
        <div>
          <el-table
            v-loading="loading"
            height="600"
            :data="traineesTableData"
            border
            style="width: 100%"
          >
            <template #empty>
              <el-empty
                v-show="!traineesTableData?.length"
                description="描述文字"
              >
              </el-empty>
            </template>
            <el-table-column prop="number" label="序号" width="180">
              <template #default="{ row }">
                {{ row.id }}
              </template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="180">
            </el-table-column>
            <!-- <el-table-column
              prop="trainingStudy"
              label="培训或学习名称"
              width="180"
            ></el-table-column> -->
            <el-table-column prop="post" label="岗位"> </el-table-column>
            <el-table-column prop="sign" label="签到">
              <template #default="{ row }">
                <div>
                  <img
                    style="width: 50px; height: 50px"
                    :src="row.signImage"
                    alt=""
                  />
                </div>
                <div>
                  {{ new Date(row.sign).toLocaleString() }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="testPaper" label="试卷附件">
              <template #default="{ row }">
                <el-link
                  type="primary"
                  v-show="row.testPaper != null"
                  :href="row.testPaper"
                  target="_blank"
                  >查看</el-link
                >
              </template>
            </el-table-column>
            <el-table-column prop="result" label="考核结果"></el-table-column>
            <el-table-column prop="qualified" label="是否合格">
              <template #default="{ row }">
                <div v-show="row.qualified == 1">合格</div>
                <div v-show="row.qualified == 0">不合格</div>
              </template>
            </el-table-column>

            <el-table-column width="120" label="操作">
              <template #default="{ row }">
                <el-popconfirm
                  style="margin-right: 10px"
                  title="确定删除吗？"
                  @confirm="deleteSafetyRow(row.id)"
                >
                  <el-button
                    slot="reference"
                    type="danger"
                    icon="el-icon-delete"
                    circle
                  ></el-button>
                </el-popconfirm>
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  @click="editSafetyRow(row.id)"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div
        style="
          width: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          height: 50px;
          margin: 0;
        "
      >
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 15, 50, 100, 200, 300, 400, 1000]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
      <el-dialog
        title="参与培训人员信息填写"
        :visible.sync="traineesVisible"
        width="50%"
        center
      >
        <el-form :model="traineesDialogForm" ref="ruleForm" label-width="auto">
          <el-row :gutter="30">
            <!-- <el-col :span="12">
              <el-form-item label="序号：" prop="number">
                <el-input
                  placeholder="请输入序号"
                  v-model="traineesDialogForm.number"
                >
                </el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span="12">
              <el-form-item label="姓名：" prop="name">
                <el-input
                  placeholder="请输入姓名"
                  v-model="traineesDialogForm.name"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <el-form-item label="培训或学习名称：" prop="trainingStudy">
                <el-input
                  v-model="traineesDialogForm.trainingStudy"
                  placeholder="请输入培训或学习名称"
                ></el-input>
              </el-form-item>
            </el-col> -->
            <!-- <el-col :span="12"> -->
            <!-- <el-form-item label="试卷附件：" prop="testPaper"> -->
            <!-- <el-link type="primary">上传附件</el-link> -->
            <!-- <el-upload
                  v-model="traineesDialogForm.testPaper"
                  :file-list="fileList4"
                  :on-success="safetyResponsibilityLetterSuccess"
                  :on-remove="safetyResponsibilityLetterRemove"
                  class="upload-demo"
                  action="/api/upload"
                  :on-error="fileError"
                  :limit="limit"
                >
                  <el-button type="primary">上传责任书</el-button>
                </el-upload>
              </el-form-item>
            </el-col> -->
            <!-- <el-col :span="12">
              <el-form-item label="签到：" prop="sign">
                <img style="width: 50px; height: 50px" :src="signUrl" alt="" />
                <div>2024-03-14 17:09</div>
              </el-form-item>
            </el-col> -->
            <el-col :span="12">
              <el-form-item label="岗位" prop="post">
                <el-input
                  placeholder="请输入岗位"
                  v-model="traineesDialogForm.post"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="考核结果：" prop="result">
                <el-input v-model="traineesDialogForm.result"> </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否合格" prop="qualified">
                <el-radio-group v-model="traineesDialogForm.qualified">
                  <el-radio :label="1" size="large">是</el-radio>
                  <el-radio :label="2" size="large">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitSecurity()">确 定</el-button>
          <el-button @click="traineesVisible = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineComponent } from "vue";
import { Notification } from "element-ui";
const props = defineProps();
const message = "Hello, Vue 3!";
const NewComponentName = defineComponent({
  name: "TraineesTable",
  setup() {
    return {
      message,
    };
  },
});

// 使用props和NewComponentName，以避免ESLint报错
console.log(props);
console.log(NewComponentName);
import { ref, onBeforeMount } from "vue";
import { getTrainees, deleteTrainees, reviseTrainees } from "@/api/trainess";
const total = ref(0);
const page = ref(1);
const size = ref(10);
const loading = ref(false);
// const limit = ref(1);
const fileList4 = ref([]);
const selectValue = ref("3");
const list = ref([
  { select: "qualified", value: "null" },
  { select: "qualified", value: "notnull" },
  { select: null, value: null },
]);
console.log(list);
const change = async () => {
  page.value = 1;
  changeTable();
};
const changeTable = async () => {
  console.log(selectValue);
  loading.value = true;
  let result = await getTrainees(
    page.value,
    size.value,
    list.value[Number(selectValue.value) - 1]?.select,
    list.value[Number(selectValue.value) - 1]?.value
  );

  traineesTableData.value = result?.data;
  total.value = Number(result?.message);
  if (isNaN(total.value)) {
    total.value = 0;
  }
  loading.value = false;
};

const handleSizeChange = async (val) => {
  size.value = val;
  page.value = 1;
  changeTable();
};
const handleCurrentChange = async (val) => {
  page.value = val;
  changeTable();
};
onBeforeMount(async () => {
  changeTable();
});

// // 安全责任书移除
// const safetyResponsibilityLetterRemove = (file) => {
//   console.log(file.url);
//   fileList4.value.map((item, index) => {
//     if (item.url == file.url) {
//       fileList4.value.splice(index, 1);
//     }
//   });
// // };
// //安全责任书文件上传
// const safetyResponsibilityLetterSuccess = (res) => {
//   if (res.code == 0) {
//     Notification({
//       title: "成功",
//       message: "上传成功",
//       type: "success",
//     });
//     fileList4.value.push({
//       name: res.data[1],
//       url: "api/" + res.data[0],
//     });
//     traineesDialogForm.value.testPaper = "api/" + res.data[0];
//   } else {
//     Notification({
//       title: "失败",
//       message: "上传失败",
//       type: "warning",
//     });
//   }
// };
// const fileError = () => {
//   Notification({
//     title: "失败",
//     message: "上传失败",
//     type: "warning",
//   });
// };
//单选框
const traineesVisible = ref(false);
//dialog表单
const traineesDialogForm = ref({
  number: null, //序号
  name: null, //姓名
  post: null, //"岗位
  sign: null, //签到
  testPaper: null, //试卷附件
  result: null, //考核结果
  qualified: null, //是否合格
  trainingStudy: null, //培训或学习名称
  signImage: null, //签到图片
});
const traineesTableData = ref([]);
//编辑行 安全
const editSafetyRow = (id) => {
  const item = traineesTableData.value.find((item) => item.id == id);
  if (item) {
    traineesDialogForm.value = { ...item };
    traineesDialogForm.value.type = "edit";
    fileList4.value = [];

    traineesDialogForm.value.appendix != null
      ? fileList4.value.push({
          name: traineesDialogForm.value.appendix,
        })
      : "";
    traineesVisible.value = true;
  } else {
    console.error("Item with id " + id + " not found.");
  }
};
// toto
const deleteSafetyRow = async (id) => {
  let result = await deleteTrainees(id);
  if (result.code == 0) {
    Notification({
      title: "成功",
      message: "删除成功",
      type: "success",
    });
    changeTable();
  } else {
    Notification({
      title: "失败",
      message: "删除失败",
      type: "warning",
    });
  }
};

const submitSecurity = async () => {
  traineesVisible.value = false;
  if (traineesDialogForm.value.type === "edit") {
    fileList4.value.length == 0
      ? (traineesDialogForm.value.appendix = null)
      : "";
    let result = await reviseTrainees(traineesDialogForm.value);
    console.log(traineesDialogForm.value);
    if (result.code == 0) {
      Notification({
        title: "成功",
        message: "修改成功",
        type: "success",
      });
      changeTable();
    } else {
      Notification({
        title: "失败",
        message: "修改失败",
        type: "error",
      });
    }
  }
  // 重置 traineesDialogForm 为初始状态
  clearLoading();
};
const clearLoading = () => {
  //dialog表单
  traineesDialogForm.value = {
    id: null, // 清除 id 或设置为初始值
    name: null, //姓名
    post: null, //"岗位
    sign: null, //签到
    testPaper: null, //试卷附件
    result: null, //考核结果
    qualified: null, //是否合格，默认为是
    trainingStudy: null, //培训或学习名称
  };
};
</script>
<style lang="scss" scoped>
.dialog-footer {
  padding: 10em;
}

.el-range-editor.el-input__inner {
  width: 240px;
}

:deep(.el-input__inner) {
  height: auto !important;
}
// :deep(.cell) {
//   display: flex;
//   justify-content: space-around;
// }
::v-deep .el-upload-list__item {
  transition: none !important;
}
::v-deep .el-upload-list__item-thumbnail {
  /* 图片在方框内显示长边 */
  object-fit: scale-down !important;
}

:deep(.el-input__inner) {
  height: auto !important;
}
:deep(.el-icon-arrow-right:before) {
  font-size: 12px !important;
}
:deep(.el-table th) {
  background: #fafafa;
}
:deep(.el-collapse-item__header) {
  font-weight: bold;
  color: #333;
  font-size: 15px;
  padding: 0 10px;
}
:deep(.el-icon-arrow-right:before) {
  font-weight: bold;
  color: #333;
  font-size: 25px;
}
body {
  min-height: 100vh;

  transition: all 0.3s ease;
}
.container {
  height: 100%;
  padding: 15px;
  overflow: auto;
  box-sizing: border-box;

  .main-content {
    background-color: #fff;
    border: 1px solid #e6e6e6;
    border-radius: 6px;
    min-width: 700px;

    .header-container {
      padding: 15px 15px;
      border-bottom: solid 1px #e6e6e6;
      box-sizing: border-box;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      flex-wrap: wrap;

      .title {
        position: relative;
        font-weight: bold;
        color: #333;
        font-size: 15px;
        padding-left: 16px;

        &::before {
          content: "";
          width: 3px;
          height: 16px;
          background-color: #82a848;
          left: 5px;
          top: calc(50% - 8px);
          position: absolute;
        }

        &::after {
          content: "";
          width: 3px;
          height: 16px;
          background-color: #82a848;
          left: 5px;
          top: calc(50% - 8px);
          position: absolute;
        }
      }
    }
  }
}

.main {
  padding: 12px 15px;

  .item-content {
    height: 100%;
    overflow: hidden;
    border-radius: 4px;
    box-sizing: border-box;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    margin-bottom: 20px;

    .item-header {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 10px;

      .subheading {
        position: relative;
        color: #555;
        font-size: 14px;
        padding-left: 14px;

        &::after {
          content: "";
          width: 2px;
          height: 14px;
          background-color: #82a848;
          left: 0;
          top: calc(50% - 7px);
          position: absolute;
        }

        &::before {
          content: "";
          width: 2px;
          height: 14px;
          background-color: #82a848;
          left: 4px;
          top: calc(50% - 7px);
          position: absolute;
        }
      }
    }

    .content-container {
      margin-top: -1px;
    }
  }
}

.w-100 {
  width: 100%;
}

.item-form-content {
  display: flex;
  flex-direction: row;
  align-items: center;

  i {
    margin: 0 10px;
  }
}

.productList-content {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  height: 40px;
}

.form-content {
  padding: 0 10px;
}

.dialog-action-bar {
  text-align: center;
  margin-top: 30px;
}
</style>
